/*
 * DocumentImpl.cpp
 *
 *  Created on: Apr 19, 2009
 *      Author: wrice
 */

#include "DOMCoreImp.h"
namespace DOMCoreImp {

DocumentImp::DocumentImp()
: NodeImp("#document", NULL)
{
}

unsigned short DocumentImp::getNodeType() {
	return Node::DOCUMENT_NODE;
}

DOMString DocumentImp::getNodeValue() throw(DOMExceptionPtr) {
	return "";
}

void DocumentImp::setNodeValue(DOMString) throw(DOMExceptionPtr) {
	throw DOMExceptionPtr(new DOMExceptionImp(DOMException::NOT_SUPPORTED_ERR));
}

Element * DocumentImp::getDocumentElement() {
	if( false == hasChildNodes() ) return NULL;
	return dynamic_cast<Element *>(getFirstChild());
}

Element * DocumentImp::createElement(DOMString tagName) throw(DOMExceptionPtr) {
	return createElementImp( tagName );
}

Attr * DocumentImp::createAttribute(DOMString name) throw(DOMExceptionPtr) {
	return createAttributeImp( name );
}

Text * DocumentImp::createTextNode(DOMString data) {
	return createTextNode( data );
}

ElementImp * DocumentImp::createElementImp(DOMString tagName) throw(DOMExceptionPtr) {
	ElementImp * newElementImp = new ElementImp(tagName, this);
	elementImps_.push_back( newElementImp );
	return newElementImp;
}

AttrImp * DocumentImp::createAttributeImp(DOMString name) throw(DOMExceptionPtr) {
	AttrImp * newAttrImp = new AttrImp(name, this);
	attrImps_.push_back( newAttrImp );
	return newAttrImp;
}

TextImp * DocumentImp::createTextNodeImp(DOMString data) {
	TextImp * newTextImp = new TextImp( data, this );
	textImps_.push_back( newTextImp );
	return newTextImp;
}

NodeList * DocumentImp::getElementsByTagName(DOMString tagname) {
	// TODO
	return NULL;
}

Node * DocumentImp::insertBefore( Node * newChild, Node * refChild ) throw(DOMExceptionPtr) {
	throw DOMExceptionImp(DOMException::NO_MODIFICATION_ALLOWED_ERR);
}

Node * DocumentImp::replaceChild( Node * newChild, Node * oldChild ) throw (DOMExceptionPtr) {
	throw DOMExceptionImp(DOMException::NO_MODIFICATION_ALLOWED_ERR);
}

Node * DocumentImp::appendChild( Node * newChild) throw(DOMExceptionPtr) {
	if( hasChildNodes() ) return NULL;
	if( newChild->getNodeType() != Node::ELEMENT_NODE ) return NULL;
	return NodeImp::appendChild( newChild );
}

AbstractView * DocumentImp::getDefaultView() {
	return NULL;
}

void DocumentImp::setDefaultView(AbstractView * view) {

}

NodeIterator * DocumentImp::createNodeIterator(
						Node * root,
						unsigned long whatToShow,
						NodeFilter * filter,
						bool entityReferenceExpansion) throw(DOMExceptionPtr)
{
	NodeIteratorImp * nodeIterator = new NodeIteratorImp( root, whatToShow, filter, entityReferenceExpansion );
	nodeIterators_.push_back( nodeIterator );
	return nodeIterator;
}

TreeWalker * DocumentImp::createTreeWalker(
						Node * root,
						unsigned long whatToShow,
						NodeFilter * filter,
						bool entityReferenceExpansion) throw(DOMExceptionPtr)
{
	throw DOMExceptionPtr(new DOMExceptionImp(DOMException::NOT_SUPPORTED_ERR));
}

}
